package com.job;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 开发公司：联信
 * 版权：联信
 * <p>
 * Annotation
 *
 * @author 刘志强
 * @created Create Time: 2021/3/2
 */
@SpringBootApplication
public class JobApplication implements CommandLineRunner {

    @Resource
    private RestTemplate restTemplate;

    public static void main(String[] args) {
        SpringApplication.run(JobApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        System.out.println(addXxlJob());
    }

    public String getCookie() {
        String path = "http://101.37.152.195:8080/xxl-job-admin"+ "/login";

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/x-www-form-urlencoded");
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>(2);
        postParameters.add("userName", "admin");
        postParameters.add("password", "123456");

        HttpEntity<Map> requestEntity = new HttpEntity<>(postParameters, requestHeaders);

        ResponseEntity<String> entity = restTemplate.exchange(path, HttpMethod.POST, requestEntity, String.class);
        return entity.getHeaders().get("Set-Cookie").get(0);
    }

    public String addXxlJob() {
        String path = "http://101.37.152.195:8080/xxl-job-admin" + "/jobinfo/add";
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/x-www-form-urlencoded");
        requestHeaders.add("Cookie",getCookie());
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>(2);
        postParameters.add("jobGroup", 2);
        postParameters.add("jobDesc", "这是测试任务");
        postParameters.add("executorRouteStrategy", "FIRST");
        postParameters.add("scheduleConf", "0 * * * * ?");
        postParameters.add("cronGen_display", "0 * * * * ?");
        postParameters.add("glueType", "BEAN");
        postParameters.add("executorHandler", "test");
        postParameters.add("executorBlockStrategy", "SERIAL_EXECUTION");
        postParameters.add("executorTimeout", 0);
        postParameters.add("executorFailRetryCount", 1);
        postParameters.add("author", "admin");
        postParameters.add("glueRemark", "GLUE代码初始化");
        postParameters.add("triggerStatus", 1);
        postParameters.add("scheduleType","CRON");
        postParameters.add("misfireStrategy","DO_NOTHING");
        HttpEntity<Map> requestEntity = new HttpEntity<>(postParameters, requestHeaders);

        ResponseEntity<String> entity = restTemplate.exchange(path, HttpMethod.POST, requestEntity, String.class);
        return entity.getBody();
    }
}
